//
//  GoOutSpeciallTopicAllViewController.m
//  ArtOfLiving
//
//  Created by 刘虎 on 16/5/1.
//  Copyright © 2016年 刘虎. All rights reserved.
//

#import "GoOutSpeciallTopicAllViewController.h"
#import "GoOutSpecialTopicDetialViewController.h"
#import "GoOutMainheadViewModel.h"

@interface GoOutSpeciallTopicAllViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableDictionary *parameters;              //请求数据参数
@property (nonatomic, strong) NSMutableArray *dataSourceMArr;               //请求回来的数据
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation GoOutSpeciallTopicAllViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    self.tabBarController.tabBar.hidden = YES;
    [self fetchDataSourceWithUrlString:kSpecrialTopicAll parameter:self.parameters];
}


- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - -----tableView的代理方法----
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSourceMArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BaseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GoOutSpecialTopicAllCELL" forIndexPath:indexPath];
    [cell setCellWithModel:self.dataSourceMArr[indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 200.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    GoOutMainheadViewModel *model = self.dataSourceMArr[indexPath.row];
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    GoOutSpecialTopicDetialViewController *goOutSpecialTopicDetialVC = [mainStoryboard instantiateViewControllerWithIdentifier:@"GoOutSpecialTopicDetialViewController"];
    goOutSpecialTopicDetialVC.htmlUrl = model.url;
    goOutSpecialTopicDetialVC.navcTitle = model.title;
    [self.navigationController pushViewController:goOutSpecialTopicDetialVC animated:YES];
}

#pragma mark - -----数据请求-----
- (void)fetchDataSourceWithUrlString:(NSString *)urlString parameter:(NSMutableDictionary *)parameter {
    
    [LHDataRequestTools getFetchDataWithUrlString:urlString paramenters:self.parameters success:^(id data) {
        
        NSDictionary *resultData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        for (NSDictionary *dic in resultData[@"data"]) {
            
            GoOutMainheadViewModel *model = [[GoOutMainheadViewModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataSourceMArr addObject:model];
        }
        if ([self.tableView.mj_header isRefreshing]) {
            
            [self.dataSourceMArr removeObjectsInRange:NSMakeRange(0, self.dataSourceMArr.count-20)];
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
    } fail:^{
        
        NSLog(@"旅游专题集数据请求失败");
    } view:[self.tableView.mj_header isRefreshing]?nil:self.view];
}



#pragma mark - ----属性懒加载-----
- (NSMutableDictionary *)parameters {
    
    if (!_parameters) {
        
        _parameters = [@{@"client_id":@"qyer_ios", @"client_secret":@"cd254439208ab658ddf9", @"count":@20, @"lat":@"40.03623116203175", @"lon":@"116.363819066288", @"oauth_token":@"2c8a27f98da55363b7f5ca09940dc14c", @"page":@1, @"track_app_channel":@"App%2520Store", @"track_app_version":@6.8, @"track_device_info":@"iPhone7%2C1", @"track_deviceid":@"7B6FA080-F9FB-44C1-B932-401CD69CD5D2", @"track_os":@"ios%25209.1", @"track_user_id":@"6971539", @"v":@1} mutableCopy];
    }
    return _parameters;
}

- (NSMutableArray *)dataSourceMArr {
    
    if (!_dataSourceMArr) {
        
        _dataSourceMArr = [[NSMutableArray alloc] init];
    }
    return _dataSourceMArr;
}

- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, self.view.frame.size.height) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:@"GoOutSpecialTopicAllCell" bundle:nil] forCellReuseIdentifier:@"GoOutSpecialTopicAllCELL"];
        _tableView.mj_footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
            
            NSInteger page = [[self.parameters objectForKey:@"page"] integerValue];
            page += 1;
            [self.parameters setObject:[NSNumber numberWithInteger:page] forKey:@"page"];
            [self fetchDataSourceWithUrlString:kSpecrialTopicAll parameter:self.parameters];
        }];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [self.parameters setObject:@1 forKey:@"page"];
            [self fetchDataSourceWithUrlString:kSpecrialTopicAll parameter:self.parameters];
        }];
    }
    return _tableView;
}


@end
